<?php


namespace app\common\model;



use Overtrue\Socialite\SocialiteManager;
use thans\jwt\facade\JWTAuth;
use think\facade\Cookie;

class Login{

    //mp
    private $appid = 'wxffe892eed3841137';
    private $secret = '22b45337266faf18039f45ef4ad972b3';
    //web
    private $appid2 = 'wx61fa9745ff24b8cc';
    private $secret2 = '4dae971918b116322d40e1f25b0eb76a';
    private $err_msg = '';
    private $register_type = '';


    public function wechat_config($callback=''){
        return [
            'wechat' => [
                'client_id'     => $this->appid,
                'client_secret' => $this->secret,
                'redirect'      => $callback,
            ],
        ];
    }

    public function wechat_config2($callback=''){
        return [
            'wechat' => [
                'client_id'     => $this->appid2,
                'client_secret' => $this->secret2,
                'redirect'      => $callback,
            ],
        ];
    }

    public function doOauthWechatLogin($code){
        $socialite = new SocialiteManager($this->wechat_config());
        try {
            $accessToken = $socialite->driver('wechat')->getAccessToken($code);
            $user = $socialite->driver('wechat')->user($accessToken);
            $weixin_info = $user->jsonSerialize();
        } catch (\Exception $e) {
            $this->error_msg = $e->getMessage();
            return false;
        }

        if(isset($weixin_info['original']['unionid'])){
            $user_id = $this->getUserIdByUnionId($weixin_info['original']['unionid']);
            if($user_id){
                return  $user_id;
            }else{
                $this->register_type = 'wechat_mp';
                return $this->register($weixin_info['nickname'],$weixin_info['avatar'],$weixin_info['original']['openid'],$weixin_info['original']['unionid'],$this->appid);
            }

        }else{
            $this->err_msg = '参数错误';
            return false;
        }
    }

    public function doOauthWechatLogin2($code){
        $socialite = new SocialiteManager($this->wechat_config2());
        try {
            $accessToken = $socialite->driver('wechat')->getAccessToken($code);
            $user = $socialite->driver('wechat')->user($accessToken);
            $weixin_info = $user->jsonSerialize();
        } catch (\Exception $e) {
            $this->err_msg = $e->getMessage();
            return false;
        }

        if(isset($weixin_info['original']['unionid'])){
            $user_id = $this->getUserIdByUnionId($weixin_info['original']['unionid']);
            if($user_id){
                return  $user_id;
            }else{
                $this->register_type = 'wechat_web';
               return $this->register($weixin_info['nickname'],$weixin_info['avatar'],$weixin_info['original']['openid'],$weixin_info['original']['unionid'],$this->appid2);
            }

        }else{
            $this->err_msg = '参数错误';
            return false;
        }

    }



    public function register($nickname,$avatar,$openid,$unionid,$appid){

        $user_data = [
            'nickname'              => $nickname,
            'icon'                  => $avatar,
            'openid'                => $openid,
            'unionid'               => $unionid,
            'appid'               => $appid,
            'user_type'             => 1,
            'mobile_verify'         => 0,
            'register_type'         => $this->register_type,
            'created_at'            => TIMESTAMP,
            'updated_at'            => TIMESTAMP,
        ];
        $memberModel = new Member();
        return $memberModel->insertGetId($user_data);
    }

    public function getUserIdByUnionId($union_id){
        $memberModel = new Member();
        return $memberModel->where('unionid',$union_id)->value('id');
    }

    public function web($member_id){
        $token = JWTAuth::builder(['user_id'=>$member_id]);
        Cookie::set('token',$token);
        Cookie::set('user_id',$member_id);
    }

}